//
//  LuckySettingWnd.m
//  SMTools
//
//  Created by fengsh on 2018/12/26.
//  Copyright © 2018年 fengsh. All rights reserved.
//

#import "LuckySettingWnd.h"
#import "LuckyApi.h"
#import "LuckyUtils.h"

@interface LuckySettingWnd ()
@property (weak) IBOutlet NSTextField *tf_name;
@property (weak) IBOutlet NSTextField *tf_members;
@property (weak) IBOutlet NSComboBox *cmb_count;
@property (weak) IBOutlet NSTextField *tf_content;
@property (weak) IBOutlet NSTextField *tf_remark;
@property (weak) IBOutlet NSButton *ckb_all;

@end

@implementation LuckySettingWnd

- (void)windowDidLoad {
    [super windowDidLoad];
    
    [self.cmb_count removeAllItems];
    for (int i=1; i <= 10; i++) {
        [self.cmb_count addItemWithObjectValue:@(i)];
    }
    
    [self.cmb_count selectItemAtIndex:0];
    
    [self.window setMinSize:NSMakeSize(560, 450)];
}

- (void)dealloc
{
    NSLog(@"LuckySettingWnd free.");
}

- (BOOL)chekok
{
    if (_tf_name.stringValue.length == 0) {
        [LuckyUtils lucky_show:@"请输入奖项名称"];
        return NO;
    }
    if (_tf_members.stringValue.length == 0) {
        [LuckyUtils lucky_show:@"请输入奖项名额"];
        return NO;
    }
    if (_cmb_count.stringValue.length == 0) {
        [LuckyUtils lucky_show:@"请输入抽奖轮次"];
        return NO;
    }
    if (_tf_content.stringValue.length == 0) {
        [LuckyUtils lucky_show:@"请输入奖项内容"];
        return NO;
    }
    return YES;
}

- (IBAction)onSaveClicked:(id)sender
{
    if (![self chekok]) {
        return;
    }
    
    NSString *name = _tf_name.stringValue;
    NSString *members = _tf_members.stringValue;
    NSString *count = _cmb_count.stringValue;
    NSString *content = _tf_content.stringValue;
    NSString *remark = _tf_remark.stringValue;
    
    NSControlStateValue st = _ckb_all.state;
    
    NSString  *scope = (st == 1) ? @"1" : @"2";
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if (name.length > 0) {
        [param setObject:name forKey:@"prizeLevelName"];
    }
    
    [param setObject:@([count integerValue]) forKey:@"count"];
    [param setObject:@([members integerValue]) forKey:@"number"];
    if (content.length > 0) {
        [param setObject:content forKey:@"prizeName"];
    }
    
    if (remark.length > 0) {
        [param setObject:remark forKey:@"company"];
    }
    
    [param setObject:scope forKey:@"scope"];
    
    [[LuckyApi api]lucky_api_post_addActive:param comletion:^(id respobj, NSError *error) {
        if (!error) {
            NSString *retcode = respobj[@"retcode"];
            if ([retcode isEqualToString:@"00000"]) {
                [self.window close];
                [[NSNotificationCenter defaultCenter]postNotificationName:@"kNtf_Refresh_List" object:nil];
            } else {
                NSString *msg = respobj[@"msg"];
                [LuckyUtils lucky_show:msg];
            }
        } else {
            [LuckyUtils lucky_show:error.localizedDescription];
        }
    }];
    
    NSLog(@"保存!,%@",param);
}

- (BOOL)windowShouldClose:(id)sender
{
//    [NSApp stopModalWithCode:1];
    return YES;
}

- (void) windowWillClose:(NSNotification *)notification
{
//    [NSApp stopModalWithCode:0];
}



@end
